<?php
namespace App\Http\Controllers\Api;

use App\Wen\Utils\Settings;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class LocationController extends BaseController
{
    public function geocode_regeo(Request $request)
    {
        $longitude = $request->longitude;
        $latitude = $request->latitude;

        $poitype = $request->poitype;
        $radius = $request->radius;

        if(_empty_($latitude) || _empty_($longitude)){
            return $this->fail(200001);
        }

        if(_empty_($radius)){
            $radius = 1000;
        }else{
            $radius = (int)$radius;
        }

        if(_empty_($poitype)){
            $poitype = null;
        }

//        if(!in_array($poitype, ['高等院校','中学', '小学', '幼儿园', '成人教育', '职业技术学校'])){
//            $poitype = null;
//        }

        $poitype_str = '';
        if($poitype){
            $poitype_str = '&poitype='.$poitype;
        }

        $web_key = Settings::get('app_gaode_web_server_key', '');
        $url = 'https://restapi.amap.com/v3/geocode/regeo?key='.$web_key.'&radius='.$radius.$poitype_str.'&extensions=all&location='.$longitude.','.$latitude;
        $post = Http::get($url);
        if($post->successful()){
            $res = $post->json();
            if(_array_key($res, 'status', -1) == 0){
                return $this->fail(200006, [], $res['info']);
            }else if(_array_key($res, 'status', -1) == 1){
                return $this->success(['infocode'=>$res['infocode'], 'regeocode'=>$res['regeocode']]);
            }else{
                _logger_(__file__, __line__, $res);
            }
        }else{
            return $this->fail(200006, [], '服务端api请求出错');
        }
    }
}
